package com.cskaoyan.servlet;

import com.cskaoyan.bean.MarketRegion;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.RegionVo;
import com.cskaoyan.service.RegionService;
import com.cskaoyan.service.RegionServiceImpl;
import com.cskaoyan.util.ResponseUtil;
import com.cskaoyan.util.URIUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author stone
 * @date 2023/03/17 15:50
 */
@WebServlet("/admin/region/*")
public class AdminRegionServlet extends HttpServlet {

    private RegionService regionService = new RegionServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String operation = URIUtil.getOperation(this.getClass(), req);
        switch (operation) {
            case "/list":
                list(req, resp);
                break;
        }
    }

    private void list(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<RegionVo> regionVoList = new ArrayList<>();

        List<MarketRegion> marketRegions = regionService.getAll();
        Map<Byte, List<MarketRegion>> collect = marketRegions.stream().collect(Collectors.groupingBy(MarketRegion::getType));
        byte provinceType = 1;
        List<MarketRegion> provinceList = collect.get(provinceType);
        byte cityType = 2;
        List<MarketRegion> city = collect.get(cityType);
        Map<Integer, List<MarketRegion>> cityListMap = city.stream().collect(Collectors.groupingBy(MarketRegion::getPid));
        byte areaType = 3;
        List<MarketRegion> areas = collect.get(areaType);
        Map<Integer, List<MarketRegion>> areaListMap = areas.stream().collect(Collectors.groupingBy(MarketRegion::getPid));

        for (MarketRegion province : provinceList) {
            RegionVo provinceVO = new RegionVo();
            provinceVO.setId(province.getId());
            provinceVO.setName(province.getName());
            provinceVO.setCode(province.getCode());
            provinceVO.setType(province.getType());

            List<MarketRegion> cityList = cityListMap.get(province.getId());
            List<RegionVo> cityVOList = new ArrayList<>();
            for (MarketRegion cityVo : cityList) {
                RegionVo cityVO = new RegionVo();
                cityVO.setId(cityVo.getId());
                cityVO.setName(cityVo.getName());
                cityVO.setCode(cityVo.getCode());
                cityVO.setType(cityVo.getType());

                List<MarketRegion> areaList = areaListMap.get(cityVo.getId());
                List<RegionVo> areaVOList = new ArrayList<>();
                for (MarketRegion area : areaList) {
                    RegionVo areaVO = new RegionVo();
                    areaVO.setId(area.getId());
                    areaVO.setName(area.getName());
                    areaVO.setCode(area.getCode());
                    areaVO.setType(area.getType());
                    areaVOList.add(areaVO);
                }

                cityVO.setChildren(areaVOList);
                cityVOList.add(cityVO);
            }
            provinceVO.setChildren(cityVOList);
            regionVoList.add(provinceVO);
        }
        ResponseUtil.responseJson(response, BaseRespVo.ok(CommonData.data(regionVoList)));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
